var bb=123;
(function(){
    var $openRule = $('#openRule'),
        $btnRedpackageH5 = $('#btnRedpackageH5'),
        $btnShareH5 = $('#btnShareH5'),
        $popRuleBox = $('#rule'),
        $closeRule = $('#closeRule'),
        $btnPlay = $('#btnPlay'),
        netState = true, // true 连接正常 false 网络断开
        videoE = document.getElementById('video'),
        $overlay = $('#model'),
        $shareImg = $('#shareImg'), // 分享的图片
        is_android = (function(){
            var u = navigator.userAgent;
            var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
            return isAndroid;
        }());
    function Page () {

    }
    Page.prototype = {
        init: function () {
            var scope = this;
            scope.initPage();
            scope.event();
        },
        event: function () {
            var scope = this;
            window.addEventListener('online',  scope.monitoringNet);
            window.addEventListener('offline', scope.monitoringNet);
            // window.onload = function () {
            //   scope.weixinShare();
            // }
            //JS绑定自动播放（操作window时，播放音乐）
            $(window).one('touchstart', function(){
                videoE.play();
            })

            //微信下兼容处理
            document.addEventListener("WeixinJSBridgeReady", function () {
                videoE.play();
            }, false);
            // 点击规则显示规则按钮
            $openRule.click(function(){
                $overlay.show();
                $popRuleBox.show();
                videoE.style.display = 'none';
            });
            // 点击关闭隐藏规则
            $closeRule.click(function(){
                $overlay.hide();
                $popRuleBox.hide();
                videoE.style.display = 'block';
            });
            // 点击分享
            $btnShareH5.click(function(){
                $overlay.show();
                $shareImg.show();
                videoE.style.display = 'none';
                setTimeout(function(){
                    $overlay.hide();
                    $shareImg.hide();
                    videoE.style.display = 'block';
                },10000);
            });
            // 点击播放按钮
            $btnPlay.click(function(e){
                e.stopPropagation();
                if(!netState){
                    alert('网络已断开');
                    return false;
                }
                videoE.play();
                $btnPlay.hide();
                if(!videoE.isPlayed){
                    $btnPlay.prev().show();
                }
                scope.report('click6');
            })
            videoE.onclick = function () {
                videoE.pause();
                $btnPlay.show();
            }
            videoE.onpause = function () {
                $btnPlay.show();
            }
            videoE.ended = function () {
                $btnPlay.show();
            }
            videoE.onplay = function () {
                videoE.play();
                $btnPlay.hide();
            }
            videoE.ontimeupdate = function(){
                if(!videoE.isPlayed){
                    //当视频的currentTime大于0.1时表示黑屏时间已过，已有视频画面，可以移除浮层
                    if (!this.isPlayed && this.currentTime>0.1 ){
                        $btnPlay.prev().fadeOut(500);
                        videoE.isPlayed=!0;
                    }
                }
            };

        },
        monitoringNet: function () {
            if (navigator.onLine) {
                alert('网络已恢复');
            } else {
                alert('网络已断开');
            }
        },
        
        initPage: function () {
              // var money = GetQueryString('j');
              var scope = this,
                  u = GetQueryString('u');
              // $.ajax({
              //         url: '//kp.dftoutiao.com/headnick?accid=100255965',
              //         dataType:'jsonp',
              //         jsonpCallback: 'jsoncallback',
              //         success:function(res){
              //             $('#avatar').append('<img src="'+res.headpic+'" alt=""/>');
              //             var nickname = res.nickname ? res.nickname+'':'';
              //             if(!isNaN(nickname)){
              //                 if(nickname.length == 11){
              //                     nickname = scope.replaceStrByPosition(nickname,3,7,'****');
              //                 }
              //             }
              //             $('#name').text(nickname);
              //         }
              // });
              // money = isNaN(money) ? '----' : money;
              // $('#money').text(money);
              var isiOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
              var data = {
                invite_code : u,
                qqid: isiOS ? "IOSeightrp_zb1" : "eightrp_zb1" ,
                from:"93014",
              };
              var m = new OpenInstall({
                  appKey : "fft561",  //formal
                  // appKey : "qpua5b",     //test
                  buttonId: "btnRedpackageH5"
              },data);

              // $('#btn02').on('click',function(){
              //       $('#model').show();
              //       $('#shareImg').show();
              //       setTimeout(function(){
              //         $('#model').hide();
              //         $('#shareImg').hide();
              //       },10000);
              // });

            function GetQueryString(name){
               var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
               var r = window.location.search.substr(1).match(reg);
               if(r!=null)return  unescape(r[2]); return null;
            }
            scope.report('show');
        },
        replaceStrByPosition: function (string, start, end, newString) {
            if (string) {
                var charArr = string.split(''),
                    len = charArr.length;
                if(len < start){
                    return '';
                }
                if(end >= len){
                    end = len - 1;
                }
                for(var i = start,j=0;i < end;i++){
                    charArr[i] = newString.charAt(j);
                    j = j + 1;
                }
                return charArr.join('');
            }
        },
        /**
         * browser的判断
         * @return {[type]} [description]
         */
        getBrowserType: function () {
            var agent = navigator.userAgent.toLowerCase();
            var browser_type = "";
            if (agent.indexOf("msie") > 0) {
                browser_type = "IE";
            }
            if (agent.indexOf("firefox") > 0) {
                browser_type = "firefox";
            }
            if (agent.indexOf("chrome") > 0 && agent.indexOf("mb2345browser") < 0 && agent.indexOf("360 aphone browser") < 0) {
                browser_type = "chrome";
            }
            if (agent.indexOf("360 aphone browser") > 0 || agent.indexOf("qhbrowser") > 0) {
                browser_type = "360";
            }
            if (agent.indexOf("ucbrowser") > 0) {
                browser_type = "UC";
            }
            if (agent.indexOf("micromessenger") > 0) {
                browser_type = "WeChat";
            }
            if ((agent.indexOf("mqqbrowser") > 0 || agent.indexOf("qq") > 0) && agent.indexOf("micromessenger") < 0) {
                browser_type = "QQ";
            }
            if (agent.indexOf("miuibrowser") > 0) {
                browser_type = "MIUI";
            }
            if (agent.indexOf("mb2345browser") > 0) {
                browser_type = "2345";
            }
            if (agent.indexOf("sogoumobilebrowser") > 0) {
                browser_type = "sogou";
            }
            if (agent.indexOf("liebaofast") > 0) {
                browser_type = "liebao";
            }
            if (agent.indexOf('weibo') > 0) {
                browser_type = "weibo";
            }
            if (agent.indexOf("safari") > 0 && agent.indexOf("chrome") < 0 && agent.indexOf("ucbrowser") < 0 && agent.indexOf("micromessenger") < 0 && agent.indexOf("mqqbrowser") < 0 && agent.indexOf("miuibrowser") < 0 && agent.indexOf("mb2345browser") < 0 && agent.indexOf("sogoumobilebrowser") < 0 && agent.indexOf("liebaofast") < 0 && agent.indexOf("qhbrowser") < 0 && agent.indexOf("weibo") < 0) {
                browser_type = "safari";
            }
            return browser_type;
        },
        report: function (type) {
            var statisData = {
                'uid': params.uid,
                'from': params.form,
                'ttaccid': params.ttaccid,
                'thisurl': location.href.replace(location.search, ''),
                'actid': 'robredpack',
                'materialid': 'zhangjingchu',
                'type': type,
                'loginid': 'null',
                'softtype': 'toutiao',
            }

            $.ajax({
                url: '//stinvi.dftoutiao.com/appstatistics/appact',
                // url: 'http://123.59.60.170/appstatistics/appact', // 测试
                data: statisData,
                type: 'GET',
                dataType: 'jsonp',
                jsonp: 'jsonpcallback',
                timeout: 5000,
                success: function (response) {
                }
            });
        }
    }
    $(function(){
        var page = new Page();
        page.init();
        FastClick.attach(document.body);
    })
}())